package com;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Email {

	public static ArrayList<String> emailList = new ArrayList<String>();
	public static Connection connection = null;

	public static void main(String[] args) {
		readFile();
		System.out.println(emailList.size());
	}
	
	public static void getEmail(String line){
		final String RE_MAIL = "([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})";
	    Pattern p = Pattern.compile(RE_MAIL);
	    Matcher m = p.matcher(line);

	    while(m.find()) {
	    	if(!emailList.contains(m.group(1))){
	    		System.out.println(m.group(1));
		        emailList.add(m.group(1));
	    	}
	    }

	}

	public static void readFile(){
		String filetoRead = "c:\\Swastik\\email.txt";

		try{
		    FileInputStream fstream = new FileInputStream(filetoRead);

		    DataInputStream in = new DataInputStream(fstream);
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));

		    String strLine ="";

		    while ((strLine = br.readLine()) != null)   {
		    	
		    	getEmail(strLine);
		    }

		    in.close();
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }
	}

	

}